package com.hainatong.shiphelper.module.QSHE.viewmodel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.api.ApiService;
import com.hainatong.shiphelper.api.HttpResult;
import com.hainatong.shiphelper.api.VMObserver;
import com.hainatong.shiphelper.mainhome.entity.WhereComBean;
import com.hainatong.shiphelper.module.QSHE.entity.QSHEDetailBean;
import com.hainatong.shiphelper.module.QSHE.entity.QSHETypeBean;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.base.LiveResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.disposables.Disposable;

public class QSHEViewModel extends BaseViewModel
{

    public QSHEViewModel(@NonNull Application application)
    {
        super(application);
    }


    private MutableLiveData<LiveResult<List<QSHEDetailBean>>> QSHEFileListResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<List<QSHEDetailBean>>> getQSHEFileListResult()
    {
        return QSHEFileListResult;
    }

    /**
     *  查询 QSHE 类别 详情
     * @param
     */
    public void queryQSHEFileDetailByDictId(String dictId)
    {
        Map<String,Object> param = new HashMap<String,Object>();

        param.put(Constants.Key.PAGE, "1");
        param.put( Constants.Key.ROWS, "50");

        List<WhereComBean> whereList = new ArrayList<>();
        whereList.add( new WhereComBean( "dict_id", dictId ) );
        param.put( "wheres", GsonUtil.obj2json( whereList ));

        ApiService.Builder.getChanBoServer()
                          .getQSHEDetailList( param )
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<HttpResult<List<QSHEDetailBean>>>()
                          {
                              @Override
                              public void onStart(Disposable d)
                              {
                                  addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getQSHEFileListResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(HttpResult<List<QSHEDetailBean>> result)
                              {
                                  //{"status":0,"msg":null,"total":1,"rows":[{"fileSuffix":".pdf","fileName":"船员服务薄 .pdf","fileSize":"449.2KB","localUrl":null,
                                  // "serverUrl":"https://qhse.oss-cn-hangzhou.aliyuncs.com/Document/59bb2b0b-780e-4cd0-ae1b-c0b8287c6bf1.pdf","version":null,"CreateDate":"2021-04-01 11:02:19","CreateID":"82cf6cd5-9101-11eb-936c-5254006b78b4","Creator":"超级管理员","Modifier":null,"ModifyDate":null,"ModifyID":null,"qhseType":"管理须知",
                                  // "showInApp":true,"dict_id":"67b98978-48c0-11eb-97c1-0c9d921bbf6e","is_public":null,"file_id":"a23ab6cf-e89a-4a84-a705-ba10c52704a1","remark":"123123","uuid":"e113de6c-f525-4a05-baac-c36062e6f55f"}],"summary":null,"extra":null}
                                  if( result.isOk() )
                                  {
                                      getQSHEFileListResult().postValue( new LiveResult<>( true , result.getRows() ) );
                                  }
                                  else
                                  {
                                      getQSHEFileListResult().postValue( new LiveResult<>( false , result.getMsg() ) );
                                  }
                              }
                          });

    }


    private MutableLiveData<LiveResult<List<QSHETypeBean>>> QSHETypeResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<List<QSHETypeBean>>> getQSHETypeResult()
    {
        return QSHETypeResult;
    }

    /**
     *  查询 QSHE 类型 列表
     */
    public void queryQSHETypeList()
    {
        //{"wheres":"[{\"name\":\"dict_type\",\"value\":\"QSHe Type\"},{\"name\":\"dict_level\",\"value\":\"1\"}]","rows":"50","page":"1"}
        Map<String,Object> param = new HashMap<String,Object>();
        param.put(Constants.Key.PAGE, "1");
        param.put( Constants.Key.ROWS, "50");
        List<WhereComBean> whereList = new ArrayList<>();
        whereList.add( new WhereComBean( "dict_type", "QSHe Type" ) );
        whereList.add( new WhereComBean( "dict_level", "1" ) );
        param.put( "wheres", GsonUtil.obj2json( whereList ));

        ApiService.Builder.getChanBoServer()
                          .getQSHETypeList(param)
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<HttpResult<List<QSHETypeBean>>>()
                          {
                              @Override
                              public void onStart(Disposable d)
                              {
                                 addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getQSHETypeResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(HttpResult<List<QSHETypeBean>> result)
                              {
                                  if( result.isOk() )
                                  {
                                      getQSHETypeResult().postValue( new LiveResult<>( true ,result.getRows() ) );
                                  }
                                  else
                                  {
                                      getQSHETypeResult().postValue( new LiveResult<>( false ,result.getMsg() ) );
                                  }
                              }
                          });

    }




}
